﻿using System;
using WYJ.Log.LogWriters;

namespace WYJ.Log.LogServices
{

    /// <summary>
    /// 单通道，因此 PauseAll() 与 Pause()、ResumeAll() 与 Resume() 相同。
    /// </summary>
    public class SinglePassageLogger: BaseLogger, ILogger
    {
        public static ILogger Use<T>(T logWriter, LogLevel logLevel = LogLevel.INFO) where T : ILogWriter => new SinglePassageLogger(logWriter, logLevel);
        public static ILogger GetLogToFileSevice(string file, LogLevel logLevel = LogLevel.INFO) => Use(new OTFWriter(file), logLevel);
        public static ILogger GetLogToConsoleSevice(LogLevel logLevel = LogLevel.INFO) => Use(new OTCWriter(), logLevel);


        protected readonly LogLevel _logLevel;
        protected readonly ILogWriter? _logWriter;

        private SinglePassageLogger(ILogWriter logWriter, LogLevel logLevel)
        {
            _logLevel = logLevel;
            _logWriter = logWriter;
        }

        protected override ILogWriter? GetLogger(LogLevel logLevel)
        {
            return logLevel < _logLevel ? null : _logWriter;
        }

        public void ResumeAll()
        {
            Resume(_logLevel);
        }

        public void PauseAll()
        {
            Pause(_logLevel);
        }

        public void Dispose()
        {
            _logWriter?.Dispose();
        }
    }
}